package com.parktech.sun.domain.service.sunhz.impl;

import com.parktech.sun.domain.model.sunhz.Enterprise;
import com.parktech.sun.domain.model.sunhz.Industry;
import com.parktech.sun.domain.model.sunhz.IndustryCategory;
import com.parktech.sun.domain.repository.BaseRepository;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.repository.sunhz.IndustryRepository;
import com.parktech.sun.domain.service.impl.BaseServiceImpl;
import com.parktech.sun.domain.service.sunhz.IndustryService;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-9
 * Time: 下午3:05
 */
@Service("industryService")
public class IndustryServiceImpl extends BaseServiceImpl<Industry> implements IndustryService {

    private IndustryRepository industryRepository;

    @Autowired
    public IndustryServiceImpl(IndustryRepository industryRepository) {
        super(industryRepository);
        this.industryRepository = industryRepository;
    }

    public Page search(String title, Integer category, Integer publishStatus, Page page) {
        Criteria criteria = industryRepository.createCriteria();
        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("title").like(title, MatchMode.ANYWHERE));
        }
/*
        if (category != -1) {
            criteria.add(Property.forName("category").eq(category));
        }
*/
        if (publishStatus != -1) {
            criteria.add(Property.forName("publishStatus").eq(publishStatus));
        }
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return industryRepository.page(criteria, page);

    }

    public List<IndustryCategory> listAllCategory() {
        Criteria criteria = industryRepository.createCriteria(IndustryCategory.class);
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("modifyTime"));
        return industryRepository.listBeans(criteria);
    }

    public Page searchCategory(String title, Page page) {

        Criteria criteria = industryRepository.createCriteria(IndustryCategory.class);
        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("name").like(title, MatchMode.ANYWHERE));
        }
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("modifyTime"));
        return industryRepository.page(criteria, page);
    }

    public IndustryCategory getByCategoryId(Long categoryId) {
        return industryRepository.getByCategoryId(categoryId);
    }

    public void saveOrUpdateCategory(IndustryCategory industryCategory) {
        industryRepository.saveOrUpdateCategory(industryCategory);
    }

    public List<Industry> listTop5Industry(Long id) {
        Criteria criteria = industryRepository.createCriteria();
        criteria.add(Property.forName("category.id").eq(id));
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        criteria.setMaxResults(5);
        return criteria.list();
    }

    public Page listIndustryByCategory(Long categoryId, Page page) {
        Criteria criteria = industryRepository.createCriteria();
        criteria.add(Property.forName("category.id").eq(categoryId));
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        return industryRepository.page(criteria, page);
    }
}
